﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using DAL_NhaSach;

namespace BLL_NhaSach
{
	public class NhanVienBLL : IProcessData
	{
		QLNhaSachEntities db;
        NhanVien nv = null;
		public NhanVienBLL(string cnStr)
		{
			db = new QLNhaSachEntities(cnStr);
		}

        public NhanVienBLL()
        {
            // TODO: Complete member initialization
        }

		public object getAllData()
		{
			return (from nv in db.NhanViens select new { nv.MaNV, nv.ChucVu.TenChucVu, nv.TenDangNhap, nv.MatKhau, nv.HoTen, nv.GioiTinh, nv.NgaySinh, nv.DiaChi, nv.SoCMND, nv.SoDienThoai, nv.Email, nv.TrangThai });
		}

		public object findObjects(string find_content)
		{
			throw new NotImplementedException();
		}
        public int getId(String user, String pass)
        {
            NhanVien nv= db.NhanViens.SingleOrDefault(p => p.TenDangNhap.Equals(user) && p.MatKhau.Equals(pass));
            if (nv != null)
            {
                return nv.MaNV;
            }
            else return -1;
        }
		public object findObjects(int find_content)
		{
			return (db.NhanViens.SingleOrDefault(p => p.MaNV == find_content));
		}

		public object sort(string Col_Name, bool state)
		{
			throw new NotImplementedException();
		}

		public bool addObject(object addObj)
		{
			NhanVien nv = (NhanVien)addObj;
			if (nv != null)
			{
				try
				{
					db.NhanViens.AddObject(nv);
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}

		public bool deleteObject(int id)
		{
			NhanVien nv = db.NhanViens.SingleOrDefault(p => p.MaNV == id);
			if (nv != null)
			{
				try
				{
					db.NhanViens.DeleteObject(nv);
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}

		public bool updateObject(object updateObj)
		{
			NhanVien nv_new = (NhanVien)updateObj;
			NhanVien nv_old = db.NhanViens.SingleOrDefault(p => p.MaNV == nv_new.MaNV);
			if (nv_old != null)
			{
				try
				{
					nv_old.TenDangNhap = nv_new.TenDangNhap;
					nv_old.MatKhau = nv_new.MatKhau;
					nv_old.HoTen = nv_new.HoTen;
					nv_old.NgaySinh = nv_new.NgaySinh;
					nv_old.MaChucVu = nv_new.MaChucVu;
					nv_old.GioiTinh = nv_new.GioiTinh;
					nv_old.DiaChi = nv_new.DiaChi;
					nv_old.Email = nv_new.Email;
					nv_old.SoCMND = nv_new.SoCMND;
					nv_old.SoDienThoai = nv_new.SoDienThoai;
					nv_old.TrangThai = nv_new.TrangThai;
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}
        public String getPassByUser(String user)
        {
            NhanVien nv = db.NhanViens.SingleOrDefault(p => p.TenDangNhap.Equals(user));
            if (nv != null)
            {
                return nv.MatKhau;
            }
            else return null;
        }
        public NhanVien dangNhap(string username, string pass)
        {
            nv = db.NhanViens.Where(n => n.TenDangNhap == username).FirstOrDefault();
            if (nv.MatKhau == pass)
            {
                return nv;
            }
            else return null;
        }
	}
}
